# 定义热门新闻前面数字颜色颜色
from flask import current_app
from flask import g
from flask import session
from functools import wraps

def index_class(index):
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ''

# 使用装饰器封装用户数据
def user_login_data(views_func):
    @wraps(views_func)
    def wrapper(*args,**kwargs):

        user_id = session.get('user_id')
        user = None
        try:
            # 局部导包，避免循环导包
            from info.models import User
            user = User.query.get(user_id)
        except Exception as e:
            current_app.logger.error(e)

        # 使用g对象存储用户登陆数据e
        g.user = user
        return views_func(*args,**kwargs)
    return wrapper

